package com.lock;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.Toast;

import com.lock.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding mainBinding;
    private int screenWidth = 0;
    private int scrollBarWidth = 0;
    private float mx;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mainBinding.getRoot());

        mainBinding.btnCheckPermission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (WindowPermissionCheck.checkPermission(MainActivity.this)) {
                    Toast.makeText(MainActivity.this, "你已开启权限", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mainBinding.img.post(new Runnable() {
            @Override
            public void run() {
                int width = mainBinding.img.getWidth();
                scrollBarWidth = mainBinding.scrollBar.getWidth();
                screenWidth = getScreenWidth(MainActivity.this);
                Log.e("TAG", "壁纸宽度：" + width);
                final int scrollWidth = width - screenWidth;
                mainBinding.scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                        Log.e("TAG", "滑动距离：" + scrollX);
                        int progress = scrollX * 10000 / scrollWidth;
                        updateScrollBar(progress);
                    }
                });
            }
        });
    }

    /**
     * 刷新滑块位置
     */
    private void updateScrollBar(int progress) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mainBinding.scrollBar.getLayoutParams();

        int b = screenWidth / scrollBarWidth;
        float f = (float) (screenWidth / 10000.00);
        int i = (int) (progress * f / (b + 1) * b);
        if (screenWidth - i <= scrollBarWidth) {//当滑块触碰到右边界停止滑块滑动
            layoutParams.setMarginStart(screenWidth - scrollBarWidth);
            mainBinding.scrollBar.setLayoutParams(layoutParams);
            return;
        }
        layoutParams.setMarginStart(i);
        mainBinding.scrollBar.setLayoutParams(layoutParams);
    }

    /**
     * 获取屏幕的宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }
}
